package com.efast.dmom.mgmt.controller.les.pda;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.mgmt.bean.les.TiltingReturnBean;
import com.efast.dmom.mgmt.entity.les.*;
import com.efast.dmom.mgmt.service.les.*;
import com.efast.dmom.mgmt.service.mes.TwBasLotService;
import com.efast.dmom.mgmt.utils.Assist;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 翻包任务头表 前端控制器
 * </p>
 *
 * @author ChenLiPeng
 * @since 2018-06-04
 */
@Controller
@RequestMapping("/twPacktranTaskHeader")
@Api(description = "pda->入库->翻包【header】")
public class TwPacktranTaskHeaderController {

	private final TwPacktranTaskDetailService twPacktranTaskDetailService;
	private final TwPacktranTaskHeaderService twPacktranTaskHeaderService;
	private final TmBasPartPackDetailService tmBasPartPackDetailService;
	private final TmBasPartPackHeaderService tmBasPartPackHeaderService;
	private final TwDocAsnDetailsService twDocAsnDetailsService;

	@Autowired
	public TwPacktranTaskHeaderController(TwPacktranTaskDetailService twPacktranTaskDetailService,
			TwPacktranTaskHeaderService twPacktranTaskHeaderService,
			TmBasPartPackDetailService tmBasPartPackDetailService,
			TmBasPartPackHeaderService tmBasPartPackHeaderService, TwDocAsnDetailsService twDocAsnDetailsService) {
		this.twPacktranTaskDetailService = twPacktranTaskDetailService;
		this.twPacktranTaskHeaderService = twPacktranTaskHeaderService;
		this.tmBasPartPackDetailService = tmBasPartPackDetailService;
		this.tmBasPartPackHeaderService = tmBasPartPackHeaderService;
		this.twDocAsnDetailsService = twDocAsnDetailsService;
	}
	@Autowired
	private TwBasLotService twBasLotService;

	@ResponseBody
	@RequestMapping(value = "/queryTask", method = RequestMethod.POST)
	@ApiOperation(value = "获取任务", notes = "请求参数：asn零件包装流水号以及任务状态")
	public ResultBean queryTask(@RequestBody TwPacktranTaskHeader twPacktranTaskHeader) {
		ResultBean resultBean = new ResultBean(true, "");
		// 【1】：根据零件包装流水号查询出翻包记录
		Map<String, Object> stringObjectMap = new HashMap<>(16);
		stringObjectMap.put("part_serial_no", twPacktranTaskHeader.getPartSerialNo());
		List<TwPacktranTaskHeader> twPacktranTaskHeaders = twPacktranTaskHeaderService.selectByMap(stringObjectMap);
		if (twPacktranTaskHeaders.size() == 0) {
			throw new ServiceException("根据条件无法查询出数据");
		}
		Map<String, Object> stringObjectMap1 = new HashMap<>(16);
		stringObjectMap1.put("tw_packtran_task_header_id", twPacktranTaskHeaders.get(0).getTwPacktranTaskHeaderId());
		stringObjectMap1.put("packtran_status", twPacktranTaskHeader.getPacktranStatus());
		List<TwPacktranTaskDetail> twPacktranTaskDetails = twPacktranTaskDetailService.selectByMap(stringObjectMap1);
		if (twPacktranTaskDetails.size() == 0) {
			throw new ServiceException("获取不到翻包任务");
		}
		int finishTilting = 0;
		for (TwPacktranTaskDetail twPacktranTaskDetail : twPacktranTaskDetails) {
			// 任务状态 默认为0 翻包任务状态 0新单 1部分翻包 2完全翻包 3任务关闭
			if ("1".equals(twPacktranTaskDetail.getPacktranStatus())
					|| "2".equals(twPacktranTaskDetail.getPacktranStatus())) {
				finishTilting++;
			}
		}
		// 【2】：在主数据中查询出改零件是否定义了翻包主数据
		Map<String, Object> stringObjectMap2 = new HashMap<>(16);
		stringObjectMap2.put("part_no_version", twPacktranTaskDetails.get(0).getPartNoVersion());
		List<TmBasPartPackHeader> tmBasPartPackHeaders = tmBasPartPackHeaderService.selectByMap(stringObjectMap2);
		if (tmBasPartPackHeaders.size() == 0) {
			throw new ServiceException(twPacktranTaskDetails.get(0).getPartNoVersion() + "在主数据中没有定义");
		}
		Map<String, Object> stringObjectMap3 = new HashMap<>(16);
		stringObjectMap3.put("tm_bas_part_pack_header_id", tmBasPartPackHeaders.get(0).getTmBasPartPackHeaderId());
		List<TmBasPartPackDetail> tmBasPartPackDetails = tmBasPartPackDetailService.selectByMap(stringObjectMap3);
		if (tmBasPartPackDetails.size() <= 0) {
			throw new ServiceException(twPacktranTaskDetails.get(0).getPartNoVersion() + "在主数据中没有定义");
		}
		// 【3】：组装返回的数据
		List<TwDocAsnDetails> listTwDocAsnDetails = twDocAsnDetailsService.selectTwDocAsnDetails(
				new Assist(Assist.andEq("part_pack_serial", twPacktranTaskHeader.getPartSerialNo())));
		if (listTwDocAsnDetails.size() == 0) {
			throw new ServiceException(twPacktranTaskHeader.getPartSerialNo() + "在asn单中没有查询到");
		}
		TiltingReturnBean tiltingReturnBean = new TiltingReturnBean();
		tiltingReturnBean.setAsnPartSerialNo(twPacktranTaskHeader.getPartSerialNo());
		tiltingReturnBean.setFinishTilting(finishTilting);
		tiltingReturnBean.setNeedTilting(twPacktranTaskDetails.size() - finishTilting);
		tiltingReturnBean.setNewCapacity(tmBasPartPackDetails.get(0).getPartQty());
		tiltingReturnBean.setOldCapacity(listTwDocAsnDetails.get(0).getAsnReceivedQty());
		tiltingReturnBean.setPackageNo(tmBasPartPackDetails.get(0).getPackNo());
		tiltingReturnBean.setPartNnamC(twPacktranTaskDetails.get(0).getPartNameC());
		tiltingReturnBean.setPartNoVersion(twPacktranTaskDetails.get(0).getPartNoVersion());
		resultBean.setObject(tiltingReturnBean);
		return resultBean;
	}

	@ResponseBody
	@RequestMapping(value = "/confirm", method = RequestMethod.POST)
	@ApiOperation(value = "确认", notes = "请求参数：{\"asnPartSerialNo\":\"xxx\",\"thisTilting\":\"\"}")
	public ResultBean turn(@RequestBody TiltingReturnBean tiltingReturnBean) {
		ResultBean resultBean = new ResultBean(true, "");
		Map<String, Object> stringObjectMap = new HashMap<>(16);
		stringObjectMap.put("part_serial_no", tiltingReturnBean.getAsnPartSerialNo());
		List<TwPacktranTaskHeader> twPacktranTaskHeaders = twPacktranTaskHeaderService.selectByMap(stringObjectMap);
		if (twPacktranTaskHeaders.size() == 0) {
			throw new ServiceException("根据条件无法查询出数据");
		}
		Map<String, Object> stringObjectMap1 = new HashMap<>(16);
		stringObjectMap1.put("tw_packtran_task_header_id", twPacktranTaskHeaders.get(0).getTwPacktranTaskHeaderId());
		EntityWrapper<TwPacktranTaskDetail> wrapper = new EntityWrapper<>();
		wrapper.where("tw_packtran_task_header_id={0}", twPacktranTaskHeaders.get(0).getTwPacktranTaskHeaderId());
		wrapper.orderBy("line_no", true);
		wrapper.last("LIMIT " + tiltingReturnBean.getThisTilting() + "");
		List<TwPacktranTaskDetail> twPacktranTaskDetails = twPacktranTaskDetailService.selectByMap(stringObjectMap1);
		for (TwPacktranTaskDetail twPacktranTaskDetail : twPacktranTaskDetails) {
			// 任务状态 默认为0 翻包任务状态 0新单 1部分翻包 2完全翻包 3任务关闭
			twPacktranTaskDetail.setPacktranStatus("2");
			twPacktranTaskDetail.setActNboundQty(twPacktranTaskDetail.getPreNboundQty());
			twPacktranTaskDetail.setActPackNum(twPacktranTaskDetail.getPrePackNum());
		}
		twPacktranTaskDetailService.updateBatchById(twPacktranTaskDetails);
		// 判断是部分翻包还是完全翻包
		TwPacktranTaskHeader twPacktranTaskHeader = new TwPacktranTaskHeader();
		twPacktranTaskHeader.setTwPacktranTaskHeaderId(twPacktranTaskDetails.get(0).getTwPacktranTaskHeaderId());
		// 完全翻包
		if (tiltingReturnBean.getThisTilting().equals(tiltingReturnBean.getNeedTilting())) {
			twPacktranTaskHeader.setPacktranStatus("2");
		}
		// 部分翻包
		if (tiltingReturnBean.getThisTilting() < tiltingReturnBean.getNeedTilting()) {
			twPacktranTaskHeader.setPacktranStatus("1");
		}
		twPacktranTaskHeaderService.updateById(twPacktranTaskHeader);

		// 确认后需要调用打印的接口，此接口由聪哥提供
		twBasLotService.printTurnOverCard(twPacktranTaskDetails);
		return resultBean;
	}

	@ResponseBody
	@RequestMapping(value = "/close", method = RequestMethod.POST)
	@ApiOperation(value = "关闭", notes = "请求参数：{\"asnPartSerialNo\":\"xxx\"}")
	public ResultBean close(@RequestBody TiltingReturnBean tiltingReturnBean) {
		ResultBean resultBean = new ResultBean(true, "");
		Map<String, Object> stringObjectMap = new HashMap<>(16);
		stringObjectMap.put("part_serial_no", tiltingReturnBean.getAsnPartSerialNo());
		List<TwPacktranTaskHeader> twPacktranTaskHeaders = twPacktranTaskHeaderService.selectByMap(stringObjectMap);
		if (twPacktranTaskHeaders.size() == 0) {
			throw new ServiceException("根据条件无法查询出数据");
		}
		Map<String, Object> stringObjectMap1 = new HashMap<>(16);
		stringObjectMap1.put("tw_packtran_task_header_id", twPacktranTaskHeaders.get(0).getTwPacktranTaskHeaderId());
		EntityWrapper<TwPacktranTaskDetail> wrapper = new EntityWrapper<>();
		wrapper.where("tw_packtran_task_header_id={0}", twPacktranTaskHeaders.get(0).getTwPacktranTaskHeaderId());
		List<TwPacktranTaskDetail> twPacktranTaskDetails = twPacktranTaskDetailService.selectByMap(stringObjectMap1);
		for (TwPacktranTaskDetail twPacktranTaskDetail : twPacktranTaskDetails) {
			// 任务状态 默认为0 翻包任务状态 0新单 1部分翻包 2完全翻包 3任务关闭
			twPacktranTaskDetail.setPacktranStatus("3");
		}
		twPacktranTaskDetailService.updateBatchById(twPacktranTaskDetails);
		return resultBean;
	}

	@ResponseBody
	@RequestMapping(value = "/release", method = RequestMethod.POST)
	@ApiOperation(value = "释放", notes = "")
	public ResultBean release(@RequestBody TiltingReturnBean tiltingReturnBean) {
		ResultBean resultBean = new ResultBean(true, "");
		return resultBean;
	}

	@ResponseBody
	@RequestMapping(value = "/print", method = RequestMethod.POST)
	@ApiOperation(value = "打印", notes = "")
	public ResultBean print(@RequestBody TiltingReturnBean tiltingReturnBean) {
		ResultBean resultBean = new ResultBean(true, "");
		return resultBean;
	}

}
